/** 
let promise = new Promise((resolve, reject) => {
    console.log("Promise")
    if (false) {
        resolve("完成了")
    } else {
        reject("失败了")
    }
})

promise.then((value) => {
    console.log(value)
},(err) => {
    console.log(err)
})

console.log("11")
*/

/** 
function timeout(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("哈哈")
        }, ms);
    })
}

timeout(1000).then((value) => {
    console.log(value)
})
*/

/** 
function getJSON(url) {
    return new Promise((resolve, reject) => {
        const handler = function() {
            if (this.readyState !== 4) {
              return;
            }
            if (this.status === 200) {
              resolve(this.response);
            } else {
              reject(new Error(this.statusText));
            }
        };
        const client = new XMLHttpRequest();
        client.open("GET", url);
        client.onreadystatechange = handler;
        client.responseType = "json";
        client.setRequestHeader("Accept", "application/json");
        client.send();
    })
}

getJSON("/posts.json").then(function(json) {
    console.log('Contents: ' + json);
  }, function(error) {
    console.error('出错了', error);
  });
*/

/** 
const p1 = new Promise(function (resolve, reject) {
    setTimeout(() => reject(new Error('fail')), 3000)
  })
  
  const p2 = new Promise(function (resolve, reject) {
    setTimeout(() => resolve(p1), 1000)
  })
  
  p2
    .then(result => console.log(result))
    .catch(error => console.log(error))
*/

/** 
const promise = new Promise(function (resolve, reject) {
    reject(new Error("fail"))
  });
  promise
  .then(function (value) { console.log(value) })
  .catch(function(err) {
    console.log("ccc"+err)
  });

console.log("ccc")
*/

setTimeout(function(){
  console.log('定时器开始啦')
});

new Promise(function(resolve){
  console.log('马上执行for循环啦');
  for(var i = 0; i < 10000; i++){
      i == 99 && resolve();
  }
}).then(function(){
  console.log('执行then函数啦')
});

console.log('代码执行结束');
